<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

//jimport('joomla.application.component.model');
jimport('joomla.application.component.modellist');

/**
 * Component Items Model
 */
class TaxibookingModelRoutes extends JModelList
{

    /**
     * Items data
     *
     * @var object
     */
    var $_data = null;
    /**
     * Items total
     *
     * @var integer
     */
    var $_total = null;
    /**
     * Pagination object
     *
     * @var object
     */
    var $_pagination = null;
    /**
     * Item id
     *
     * @var int
     */
    var $_id = null;

    /**
     * Constructor
     *
     * @since 1.0
     */
    function __construct()
    {
        parent::__construct();

        global $mainframe, $option;
//
//        $limit = 20; // $mainframe->getUserStateFromRequest( $option.'.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
//        $limitstart = 0; //$mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );
 $jconfig = new JConfig();// getting configuration class

        $limit = $this->getUserStateFromRequest($this->context . '.limit', 'limit', $jconfig->list_limit, 'int');
        $limitstart = $this->getUserStateFromRequest($this->context . '.limitstart', 'limitstart', $jconfig->list_limit, 'int');
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);

        $array = JRequest::getVar('cid', 0, '', 'array');
        $this->setId((int) $array[0]);
    }

    /**
     * Method to set the Items identifier
     *
     * @access	public
     * @param	int Category identifier
     */
    function setId($id)
    {
        // Set id and wipe data
        $this->_id = $id;
        $this->_data = null;
    }

    /**
     * Method to get item data
     *
     * @access public
     * @return object
     */
    function getData()
    {
        // Lets load the Items if it doesn't already exist
        if (empty($this->_data))
        {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
        }

        return $this->_data;
    }

    /**
     * Method to get the total nr of the Items
     *
     * @access public
     * @return integer
     */
    function getTotal()
    {
        // Lets load the Items if it doesn't already exist
        if (empty($this->_total))
        {
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);
        }

        return $this->_total;
    }

    /**
     * Method to get a pagination object for the Items
     *
     * @access public
     * @return object
     */
    function getPagination()
    {
        // Lets load the Items if it doesn't already exist
        if (empty($this->_pagination))
        {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
        }

        return $this->_pagination;
    }

    /**
     * Method to build the query for the Items
     *
     * @access private
     * @return string
     * @since 1.0
     */
    function _buildQuery()
    {
        // Get the WHERE and ORDER BY clauses for the query
        $where = $this->_buildContentWhere();
        $orderby = $this->_buildContentOrderBy();

        $query = 'SELECT p.*'
                . ' FROM #__taxibooking_routes AS p'
                . $where
                . $orderby
        ;

        return $query;
    }

    /**
     * Method to build the orderby clause of the query for the Items
     *
     * @access private
     * @return string
     * @since 1.0
     */
    function _buildContentOrderBy()
    {
//        global $mainframe, $option;

        $filter_order = $this->getUserStateFromRequest($this->context . '.routes.filter_order', 'filter_order', 'p.id', 'cmd'); //$mainframe->getUserStateFromRequest( $option.'.routes.filter_order', 		'filter_order', 	'p.ordering', 'cmd' );
        $filter_order_Dir = $this->getUserStateFromRequest($this->context . '.routes.filter_order_Dir', 'filter_order_Dir', 'DESC', 'word'); // $mainframe->getUserStateFromRequest( $option.'.routes.filter_order_Dir',	'filter_order_Dir',	'', 'word' );
        if (trim($filter_order) == '')
            $filter_order = ' p.id ';
        if (trim($filter_order_Dir) == '')
            $filter_order_Dir = ' DESC ';

        $orderby = ' ORDER BY ' . $filter_order . ' ' . $filter_order_Dir . ', p.ordering';

        return $orderby;
    }

    /**
     * Method to build the where clause of the query for the Items
     *
     * @access private
     * @return string
     * @since 1.0
     */
    function _buildContentWhere()
    {
//        global $mainframe, $option;

        $filter_state = $this->getUserStateFromRequest($this->context . '.routes.filter_state', 'filter_state', '', 'word'); //$mainframe->getUserStateFromRequest( $option.'.routes.filter_state', 'filter_state', '', 'word' );
        $search = $this->getUserStateFromRequest($this->context . '.routes.search', 'search', '', 'string');
        $search = $this->_db->getEscaped(trim(JString::strtolower($search)));


        $where = array();

        if ($filter_state)
        {

            if ($filter_state == 'P')
            {
                $where[0] = 'p.published = 1';
            }
            else if ($filter_state == 'U')
            {
                $where[0] = 'p.published = 0';
            }
        }

        if ($search)
        {
            $where[1] = ' LOWER(p.title) LIKE ' . $this->_db->Quote('%' . $this->_db->getEscaped($search, true) . '%', false);
        }

        $where = ( count($where) ? ' WHERE ' . implode(' AND ', $where) : '' );

        return $where;
    }

    /**
     * Method to (un)publish a category
     *
     * @access	public
     * @return	boolean	True on success
     * @since	1.0
     */
    function publish($cid = array(), $publish = 1)
    {
        $user = & JFactory::getUser();

        if (count($cid))
        {
            $cids = implode(',', $cid);

            $query = 'UPDATE #__taxibooking_routes'
                    . ' SET published = ' . (int) $publish
                    . ' WHERE id IN (' . $cids . ')'
            ;
            $this->_db->setQuery($query);
            if (!$this->_db->query())
            {
                $this->setError($this->_db->getErrorMsg());
                return false;
            }
        }
        return $cid;
    }

    /**
     * Method to move a Item
     *
     * @access	public
     * @return	boolean	True on success
     * @since	1.0
     */
    function move($direction)
    {
        $row = & JTable::getInstance('taxibooking_routes', '');

        if (!$row->load($this->_id))
        {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        if (!$row->move($direction))
        {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        return true;
    }

    /**
     * Method to order Items
     *
     * @access	public
     * @return	boolean	True on success
     * @since	1.0
     */
    function saveorder($cid = array(), $order)
    {
        $row = & JTable::getInstance('taxibooking_routes', '');

        // update ordering values
        for ($i = 0; $i < count($cid); $i++)
        {
            $row->load((int) $cid[$i]);

            if ($row->ordering != $order[$i])
            {
                $row->ordering = $order[$i];
                if (!$row->store())
                {
                    $this->setError($this->_db->getErrorMsg());
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * Method to remove a faq item
     *
     * @access	public
     * @return	boolean	True on success
     * @since	1.0
     */
    function delete($cid)
    {
        if (count($cid))
        {
            $cids = implode(',', $cid);
            $query = 'DELETE FROM #__taxibooking_routes'
                    . ' WHERE id IN (' . $cids . ')'
            ;

            $this->_db->setQuery($query);

            if (!$this->_db->query())
            {
                $this->setError($this->_db->getErrorMsg());
                return false;
            }

            return true;
        }
        return false;
    }

}

?>